package com.act.moudles.baseAct.controller;



import com.act.components.UserComponent;
import com.act.moudles.baseAct.entity.OnlineDeployRecords;
import com.act.moudles.baseAct.service.OnlineDeployRecordsService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.act.moudles.baseAct.entity.OnlineDeploy;
import com.act.moudles.baseAct.service.OnlineDeployService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;


/**
 * (OnlineDeploy)表控制层
 *
 * @author makejava
 * @since 2023-05-09 21:45:25
 */
@RestController
@RequestMapping("baseAct/onlineDeploy")
public class OnlineDeployController  {
    /**
     * 服务对象
     */
    @Resource
    private OnlineDeployService onlineDeployService;

    @Resource
    private OnlineDeployRecordsService recordsService;

    @Resource
    private UserComponent userComponent;


    @GetMapping("list")
    @ApiOperation("列表查询")
    public DataResult list(){
        return DataResult.success("查询出来了！",onlineDeployService.list());
    }

    /**
     * 分页查询所有数据
     * @return 所有数据
     */
    @PostMapping("page")
    public DataResult page(@RequestBody PageSearch<OnlineDeploy> pageSearch) {
        Page<OnlineDeploy> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("",onlineDeployService.pageDeploy(page,pageSearch.getDomain()));
    }

    @PostMapping("pageHis")
    public DataResult pageRecords(@RequestBody PageSearch<OnlineDeployRecords> pageSearch) {
        Page<OnlineDeployRecords> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("",recordsService.page(page,pageSearch.getDomain().query()));
    }


    @PostMapping("onlineDeploy")
    @ApiOperation("在线部署")
    public DataResult  onlineDeploy(@RequestParam("files") MultipartFile[] files,
                                    @RequestParam("deployName") String deployName,
                                    @RequestParam("isOver") Integer isOver){
        if(files.length != 2){
            return DataResult.fail("只能上传2个文件！");
        }
        //1.解析文件
        MultipartFile bpmnFile = null;
        MultipartFile pngFile = null;
        for(MultipartFile file : files){
            if(file.getOriginalFilename().endsWith("bpmn")){
                bpmnFile = file;
            }
            if(file.getOriginalFilename().endsWith("png")){
                pngFile = file;
            }
        }
        if(bpmnFile == null || pngFile == null){
            return  DataResult.fail("目前仅支持上传一个bpmn，一个png文件");
        }

        //2.验证流程是否存在
        if(isOver == 0){
            int counts = onlineDeployService.countByBpmnFileNameFromUser(bpmnFile.getOriginalFilename());
            if(counts > 0){
                return DataResult.success(false,"该流程已经存在，是否覆盖?");
            }
        }
        try {
            onlineDeployService.uploadFileAndDeploy(bpmnFile,pngFile,bpmnFile.getOriginalFilename(),deployName);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.fail("部署失败");
        }
        return DataResult.success("流程部署成功！");
    }


}

